var Event = require('../models/event');
var Record = require('../models/record');
var collegeScore = require('../models/collegeScore');

/**
 * 存储项目数据
[
    {
        "title": "1000米",
        "mode": false,
        "ranking": 8,
        "name": "江军",
        "college": "计网牛",
        "result": "3'38",
        "score": 4,
        "sex": "男"
    },
    {
        "title": "800米",
        "mode": false,
        "ranking": 4,
        "name": "罗刚",
        "college": "计网牛",
        "result": "3'08",
        "score": 9,
        "sex": "女"
    }
]
 * @param data 数组类型
 */

function saveEvents(data) {
    Event.insertMany(data, (err, docs) => {
        if (!err) {
            console.log(docs);
            return 'save ok';
        }
        else return 'insert error';
    })
}
exports.saveEvents = saveEvents;

/**
 * 保存记录方法
 * @param recordData
 * [
    {
        "title": "10000米",
        "record": "30",
        "score": 10,
        "sex": "男"
    },
    {
        "title": "1000米",
        "record": "3",
        "score": 10,
        "sex": "男"
    },
]
 */
function saveRecord(recordData) {
    Record.insertMany(recordData, (err, docs) => {
        if (!err) {
            console.log(docs);
            return 'save ok';
        }
        else return 'insert error';
    })
}
exports.saveRecord = saveRecord;

/**
 * data: [
 *     {
 *         "title": "八",
 *         "mode": true,
 *         "ranking": 8,
 *         "name": "潘敏",
 *         "college": "计网牛",
 *         "result": "10S",
 *         "score":
 *     },
 *     {
 *         "title": "100米",
 *         "mode": true,
 *         "ranking": 7,
 *         "name": "潘敏",
 *         "college": "计网牛",
 *         "result": "9S",
 *         "score":
 *     }
 * ]
 * @return data
 */
function findAllEvents() {
    return new Promise((resolve, reject) => {
        Event.find({}, { _id: 0, __v: 0 }, (err, docs) => {
            if (!err) {
                resolve(docs);
            }
            else {
                console.log(err);
                reject(err);
            }
        })
    })
}
exports.findAllEvents = findAllEvents;

/**
 * data: [
 *     {
 *         "title": "800米",
 *         "record": "3分",
 *         "score": 10
 *     },
 *     {
 *         "title": "100米",
 *         "record": "9S",
 *         "score": 10
 *     },
 *     {
 *         "title": "100米",
 *         "record": "10S",
 *         "score": 10
 *     },
 * ]
 * @return data
 */
function findAllRecord() {
    return new Promise((resolve, reject) => {
        Record.find({}, { _id: 0, __v: 0 }, (err, docs) => {
            if (!err) {
                console.log(docs);
                resolve(docs);
            }
            else reject(err);
        })
    })

}
exports.findAllRecord = findAllRecord;
